package com.kgc.ebook.ebookmanager.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.ebook.ebookmanager.ResultEntity;
import com.kgc.ebook.ebookmanager.pojo.EbookCategory;
import com.kgc.ebook.ebookmanager.pojo.EbookEntry;
import com.kgc.ebook.ebookmanager.pojo.ViewEntryCategory;
import com.kgc.ebook.ebookmanager.service.EbookService;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.transform.Result;
import java.util.List;

@RestController
public class EbookController {
    @Resource
    EbookService service;

    //查询分类列表
    @RequestMapping("/select/category")
    public ResultEntity<List<EbookCategory>> querycategory(){
        List<EbookCategory> list=service.queryAll();
        System.out.println(list);
        return ResultEntity.successWithoutData(list);
    }

    //查询图书信息
    @RequestMapping("/select/Entry")
    public PageInfo<ViewEntryCategory> queryEbook(@RequestParam(value = "categoryid",required = false,defaultValue = "0") Integer categoryid,
                                                  @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize){
        PageInfo<ViewEntryCategory> pageInfo=service.querybookAll(categoryid, pageNum, pageSize);
        System.out.println(pageInfo);
        return  pageInfo;
    }
    //添加图书信息/修改
     //前端请求post,参数是json必须加@RequestBody
    @RequestMapping("/saveEntry")
    public ResultEntity<Integer> saveEntry(@RequestBody EbookEntry ebookEntry){
        Integer integer=null;
        if(ebookEntry.getId()==null){
            //添加
            service.addEbook(ebookEntry);
        }else{
            //修改
            service.updateEbook(ebookEntry);
        }

        return ResultEntity.successWithoutData(integer);
    }
    //根据编号查询图书
    @RequestMapping("/getEntryByid")
    public ResultEntity<EbookEntry> getEntryByid(Integer id){
        EbookEntry ebookEntry = service.queryByid(id);
        return ResultEntity.successWithoutData(ebookEntry);
    }
    //删除
    @RequestMapping("/delEntryByid")
    public ResultEntity<Integer> delByid(Integer id){
       Integer result=service.delEbook(id);
        return ResultEntity.successWithoutData(result);
    }
}
